using Common;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;

// 引入 TextMeshPro 命名空间

namespace CardGameObject.GameScene
{
    public class GamePlayer : MonoBehaviour
    {
        private const int MAX_HEALTH = 30;
        private TextMeshPro textMeshPro; // 用于存储 TextMeshPro 组件的引用
        public PlayerRoleEnum PlayerRole;
        public SpriteRenderer Border;

        public int Health { get; set; }

        public int CurHealth { get; set; }

        public GameManager GameManager { get; set; }

        private void Start()
        {
            GameManager = GameManager.GetInstance();
            Health = MAX_HEALTH;
            CurHealth = MAX_HEALTH;

            // 查找用户头像子对象上的 TextMeshPro 组件
            textMeshPro = GetComponentInChildren<TextMeshPro>();
            if (textMeshPro == null)
            {
                Debug.LogError("未找到 TextMeshPro 组件，请检查用户头像的子对象是否包含该组件。");
            }
            else
            {
                // 初始化文本显示
                UpdateHealthText();
            }
        }

        private void OnMouseDown()
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }
            if (GameManager.CheckPlayerAttackable(PlayerRole))
            {
                GameManager.CurAttackRoleTarget = this.gameObject;
                GameManager.DoAttack();
            }
            else
            {
                GameManager.CloseAttackBorder();
                GameManager.ResetAttackProperties();
            }

        }

        public void Recover(int value)
        {
            CurHealth = CurHealth + value > Health ? Health : CurHealth + value;
            UpdateHealthText();
        }

        public void RecoverToMax()
        {
            CurHealth = Health;
            UpdateHealthText();
        }

        public void TakeDamage(int damage)
        {
            CurHealth = CurHealth - damage < 0 ? 0 : CurHealth - damage;
            string player = PlayerRole == PlayerRoleEnum.PLAYER ? "玩家" : "AI";
            Debug.Log($"{player}受到 {damage} 点伤害，当前生命值: {CurHealth}");
            if (textMeshPro != null)
            {
                UpdateHealthText();
            }
            if(CurHealth == 0)
            {
                if (PlayerRole == PlayerRoleEnum.PLAYER)
                    GameManager.GameEnd(PlayerRoleEnum.AI);
                else
                    GameManager.GameEnd(PlayerRoleEnum.PLAYER);
            }
        }

        private void UpdateHealthText()
        {
            // 更新 TextMeshPro 组件的文本内容
            textMeshPro.text = CurHealth.ToString();
        }

        /// <summary>
        /// 设置边框颜色为红色并显示边框
        /// </summary>
        public void SetAttackableBorder()
        {
            Color redColor = Color.red;
            redColor.a = 1f; // 设置透明度为完全不透明
            Border.color = redColor;
        }

        /// <summary>
        /// 设置边框透明，使其不再显示
        /// </summary>
        public void SetBorderTransparent()
        {
            Color transparentColor = Border.color;
            transparentColor.a = 0f; // 设置透明度为完全透明
            Border.color = transparentColor;
        }
    }
}
